package com.rachel.liveplayer.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;

import com.rachel.liveplayer.BaseActivity;
import com.rachel.liveplayer.R;
import com.rachel.utilslib.utils.SharePreferencesUtils;

/**
 * @author zhengshaorui
 */
public class SplaseActivity extends BaseActivity {

    private static final int SHOW_GLIDE = 0X001;
    private static final int SHOW_HOME = 0X002;
    private static final int DELAY_TIME = 2000;
    private static final String FILE_NAME = "config";
    private static final String DATA_KEY = "isFirstBoot";


    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SHOW_GLIDE:
                    startActivity(new Intent(SplaseActivity.this,GlideActivity.class));
                    finish();
                    break;
                case SHOW_HOME:
                    startActivity(new Intent(SplaseActivity.this,MainActivity.class));
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public int getLayoutId() {
        return R.layout.activity_splase;
    }

    @Override
    public void initView() {
        final boolean isFirst =  SharePreferencesUtils.getBooleanData(FILE_NAME,DATA_KEY);

        ImageView imageView = getView(R.id.splase_icon);

        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView,"scaleX",1.0f,1.2f,1.0f);
        animator.setDuration(2000);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.start();

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (isFirst){
                    mHandler.sendEmptyMessageDelayed(SHOW_HOME,DELAY_TIME);
                }else{
                    SharePreferencesUtils.saveBooleanData(FILE_NAME,DATA_KEY,true);
                    mHandler.sendEmptyMessageDelayed(SHOW_GLIDE,DELAY_TIME);
                }
            }
        });

    }

    @Override
    public void initData() {

    }
}
